﻿using BodaNetCommunications.Cores;
using BodaNetCommunications.FinsComs.FinsUtils;
using BodaNetCommunications.S7Coms.S7Utils;
using BodaNetCommunications.Utilities.Attributes;
using BodaNetCommunications.Utilities.ComTypes;
using BodaNetCommunications.Utilities.Results;

namespace BodaNetCommunications.FinsComs
{
    [ProtocolTypeAttribute(ProtocolType.FinsTcp)]
    public class FinsTcpCommService : CommReadWrite, IFinsTcpCommService
    {
        public ConnectionOptions Options { get; private set; }

        public bool IsConnected => Fins != null && Fins.IsConnected;

        public FinsTcp Fins { get; private set; }

        public FinsTcpCommService()
        {
            base._byteTransform = new FinsByteTransformBase();
        }


        public BodaResult Connect(ConnectionOptions connectionOptions)
        {
            Options = connectionOptions;

            if (IsConnected)
            {
                BodaResult bodaResult = new BodaResult();
                bodaResult.IsSuccess = true;
                bodaResult.Message = $"FinsTcp【{Options.Ip}:{Options.Port}】的连接已经在线，不需要重复连接";
                return bodaResult;
            }

            Fins = new FinsTcp();

            return Fins.Connect(connectionOptions);
        }

        public void Disconnect()
        {
            Fins?.Disconnect();
        }

        /// <summary>
        /// 重写基础数据的读取功能
        /// </summary>
        /// <param name="address">数据地址</param>
        /// <param name="length">数据个数</param>
        /// <returns></returns>
        public override BodaResult<byte[]> Read(string address, ushort length)
        {
            BodaResult<byte[]>? readResult = new BodaResult<byte[]>();
            if (Fins==null)
            {
                readResult.IsSuccess = false;
                readResult.Message = "FinsTcp未获取到实例";
                return readResult;
            }
            var res = Fins.Read<byte>(address, length);
           
            if (res.IsSuccess)
            {
                readResult.Content = res.Datas.ToArray();
            }
            else
            {
                readResult.IsSuccess = false;
                readResult.Message = res.Message;
            }
            return readResult;
        }

        /// <summary>
        /// TODO: 写入功能，需要进一步的优化和验证
        /// </summary>
        /// <param name="address"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override BodaResult Write(string address, byte[] value)
        {
            List<byte> bytes = new List<byte>();
            bytes.AddRange(value);
            var res = Fins.Write<byte>(address, bytes);
            return res;
        }

        public BodaResult<byte> ReceiveMsg(int timeout = 2000, int bytesLen = 1024)
        {
            throw new NotImplementedException();
        }

        public BodaResult SendMsgc(byte[] bytes, int timeout = 1500)
        {
            throw new NotImplementedException();
        }

        public void Dispose()
        {
            this.Disconnect();
        }
    }
}
